{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Collecting Player Game Logs\n",
    "===\n",
    "\n",
    "One of the primary modules in `py-goldsberry` is the `player` module. It provides access to a multitude of player-level statistics. \n",
    "\n",
    "Each class in the `player` module requires a specific **playerID**. If you have looked through the first tutorial, you can see that `py-goldsberry` has a built-in function that makes it easy to find the **playerID**s for a given season."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.0.1'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import goldsberry\n",
    "import pandas as pd\n",
    "goldsberry.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One of the many things you can do with `py-goldsberry` is generate a list of game logs for a single player or the entire league (depending on what you desire). This can be accomplished very easily using two built-in methods and a simple custom function.\n",
    "\n",
    "First, we generate a list of players from the current season using the built-in `PlayerList()` function, and convert it to a Pandas DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DISPLAY_FIRST_LAST</th>\n",
       "      <th>DISPLAY_LAST_COMMA_FIRST</th>\n",
       "      <th>FROM_YEAR</th>\n",
       "      <th>GAMES_PLAYED_FLAG</th>\n",
       "      <th>PERSON_ID</th>\n",
       "      <th>PLAYERCODE</th>\n",
       "      <th>ROSTERSTATUS</th>\n",
       "      <th>TEAM_ABBREVIATION</th>\n",
       "      <th>TEAM_CITY</th>\n",
       "      <th>TEAM_CODE</th>\n",
       "      <th>TEAM_ID</th>\n",
       "      <th>TEAM_NAME</th>\n",
       "      <th>TO_YEAR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Quincy Acy</td>\n",
       "      <td>Acy, Quincy</td>\n",
       "      <td>2012</td>\n",
       "      <td>Y</td>\n",
       "      <td>203112</td>\n",
       "      <td>quincy_acy</td>\n",
       "      <td>1</td>\n",
       "      <td>SAC</td>\n",
       "      <td>Sacramento</td>\n",
       "      <td>kings</td>\n",
       "      <td>1610612758</td>\n",
       "      <td>Kings</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Jordan Adams</td>\n",
       "      <td>Adams, Jordan</td>\n",
       "      <td>2014</td>\n",
       "      <td>Y</td>\n",
       "      <td>203919</td>\n",
       "      <td>jordan_adams</td>\n",
       "      <td>1</td>\n",
       "      <td>MEM</td>\n",
       "      <td>Memphis</td>\n",
       "      <td>grizzlies</td>\n",
       "      <td>1610612763</td>\n",
       "      <td>Grizzlies</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Steven Adams</td>\n",
       "      <td>Adams, Steven</td>\n",
       "      <td>2013</td>\n",
       "      <td>Y</td>\n",
       "      <td>203500</td>\n",
       "      <td>steven_adams</td>\n",
       "      <td>1</td>\n",
       "      <td>OKC</td>\n",
       "      <td>Oklahoma City</td>\n",
       "      <td>thunder</td>\n",
       "      <td>1610612760</td>\n",
       "      <td>Thunder</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arron Afflalo</td>\n",
       "      <td>Afflalo, Arron</td>\n",
       "      <td>2007</td>\n",
       "      <td>Y</td>\n",
       "      <td>201167</td>\n",
       "      <td>arron_afflalo</td>\n",
       "      <td>1</td>\n",
       "      <td>NYK</td>\n",
       "      <td>New York</td>\n",
       "      <td>knicks</td>\n",
       "      <td>1610612752</td>\n",
       "      <td>Knicks</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alexis Ajinca</td>\n",
       "      <td>Ajinca, Alexis</td>\n",
       "      <td>2008</td>\n",
       "      <td>Y</td>\n",
       "      <td>201582</td>\n",
       "      <td>alexis_ajinca</td>\n",
       "      <td>1</td>\n",
       "      <td>NOP</td>\n",
       "      <td>New Orleans</td>\n",
       "      <td>pelicans</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>Pelicans</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  DISPLAY_FIRST_LAST DISPLAY_LAST_COMMA_FIRST FROM_YEAR GAMES_PLAYED_FLAG  \\\n",
       "0         Quincy Acy              Acy, Quincy      2012                 Y   \n",
       "1       Jordan Adams            Adams, Jordan      2014                 Y   \n",
       "2       Steven Adams            Adams, Steven      2013                 Y   \n",
       "3      Arron Afflalo           Afflalo, Arron      2007                 Y   \n",
       "4      Alexis Ajinca           Ajinca, Alexis      2008                 Y   \n",
       "\n",
       "   PERSON_ID     PLAYERCODE  ROSTERSTATUS TEAM_ABBREVIATION      TEAM_CITY  \\\n",
       "0     203112     quincy_acy             1               SAC     Sacramento   \n",
       "1     203919   jordan_adams             1               MEM        Memphis   \n",
       "2     203500   steven_adams             1               OKC  Oklahoma City   \n",
       "3     201167  arron_afflalo             1               NYK       New York   \n",
       "4     201582  alexis_ajinca             1               NOP    New Orleans   \n",
       "\n",
       "   TEAM_CODE     TEAM_ID  TEAM_NAME TO_YEAR  \n",
       "0      kings  1610612758      Kings    2015  \n",
       "1  grizzlies  1610612763  Grizzlies    2015  \n",
       "2    thunder  1610612760    Thunder    2015  \n",
       "3     knicks  1610612752     Knicks    2015  \n",
       "4   pelicans  1610612740   Pelicans    2015  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players = goldsberry.PlayerList()\n",
    "players2015 = pd.DataFrame(players.players())\n",
    "players2015.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have the data into a DataFrame, you can take advantage of the Pandas functionality to search for specific players, teams, rookie cohorts, etc...\n",
    "\n",
    "Let's start by looking for just James Harden."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DISPLAY_FIRST_LAST</th>\n",
       "      <th>DISPLAY_LAST_COMMA_FIRST</th>\n",
       "      <th>FROM_YEAR</th>\n",
       "      <th>GAMES_PLAYED_FLAG</th>\n",
       "      <th>PERSON_ID</th>\n",
       "      <th>PLAYERCODE</th>\n",
       "      <th>ROSTERSTATUS</th>\n",
       "      <th>TEAM_ABBREVIATION</th>\n",
       "      <th>TEAM_CITY</th>\n",
       "      <th>TEAM_CODE</th>\n",
       "      <th>TEAM_ID</th>\n",
       "      <th>TEAM_NAME</th>\n",
       "      <th>TO_YEAR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>179</th>\n",
       "      <td>James Harden</td>\n",
       "      <td>Harden, James</td>\n",
       "      <td>2009</td>\n",
       "      <td>Y</td>\n",
       "      <td>201935</td>\n",
       "      <td>james_harden</td>\n",
       "      <td>1</td>\n",
       "      <td>HOU</td>\n",
       "      <td>Houston</td>\n",
       "      <td>rockets</td>\n",
       "      <td>1610612745</td>\n",
       "      <td>Rockets</td>\n",
       "      <td>2015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    DISPLAY_FIRST_LAST DISPLAY_LAST_COMMA_FIRST FROM_YEAR GAMES_PLAYED_FLAG  \\\n",
       "179       James Harden            Harden, James      2009                 Y   \n",
       "\n",
       "     PERSON_ID    PLAYERCODE  ROSTERSTATUS TEAM_ABBREVIATION TEAM_CITY  \\\n",
       "179     201935  james_harden             1               HOU   Houston   \n",
       "\n",
       "    TEAM_CODE     TEAM_ID TEAM_NAME TO_YEAR  \n",
       "179   rockets  1610612745   Rockets    2015  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players2015.ix[players2015['DISPLAY_LAST_COMMA_FIRST'].str.contains(\"Harden\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fortunately, there is only one player with `Harden` somewhere in his name. If we had searched for `James`, it would have been a bit of a different story.\n",
    "\n",
    "Because we want to get information on James Harden, we need to make note of the value in his **PERSON_ID** column. This is the unique id number that is associated with Harden in the NBA database. Anytime we want to search for James Harden related information, this will be a value to remember. \n",
    "\n",
    "To make it easy to remember, I'm going to save it as a variable in our environment that we can call it anytime we want. It's a bit easier for me to remember `harden_id` than `201935`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "harden_id = '201935'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Game Logs\n",
    "\n",
    "One of many pieces of available data for a player is their game logs. You can access these by using the `goldsberry.player.game_logs()` class and passing in the playerID. \n",
    "\n",
    "There are a few variables that can be manipulated in the game_logs to adjust the data that gets returned. The most important is the `season` argument. When you instantiate the class, you must pass a valid player id. When the class loads, it automatically grabs all of the game logs for the player for the current season. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "harden_game_logs = goldsberry.player.game_logs(harden_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we've collected the data from the NBA website, we want to create a Pandas DataFrame to view an analyze. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "harden_game_logs_2015 = pd.DataFrame(harden_game_logs.logs())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that we passed `harden_game_logs.logs()` and not `harden_game_logs` to the DataFrame constructor. This is because, with many of the calls in `py-Goldsberry`, there are multiple sets of data returned. Instead of making multiple calls to the NBA's server, a single call is made and all of the data is store in the class. The various methods of the class provide access to the raw data. \n",
    "\n",
    "(Until documentation is complete, take advantage of the [TAB] complete feature in jupyter.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AST</th>\n",
       "      <th>BLK</th>\n",
       "      <th>DREB</th>\n",
       "      <th>FG3A</th>\n",
       "      <th>FG3M</th>\n",
       "      <th>FG3_PCT</th>\n",
       "      <th>FGA</th>\n",
       "      <th>FGM</th>\n",
       "      <th>FG_PCT</th>\n",
       "      <th>FTA</th>\n",
       "      <th>...</th>\n",
       "      <th>PF</th>\n",
       "      <th>PLUS_MINUS</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Player_ID</th>\n",
       "      <th>REB</th>\n",
       "      <th>SEASON_ID</th>\n",
       "      <th>STL</th>\n",
       "      <th>TOV</th>\n",
       "      <th>VIDEO_AVAILABLE</th>\n",
       "      <th>WL</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>0.500</td>\n",
       "      <td>27</td>\n",
       "      <td>13</td>\n",
       "      <td>0.481</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>21</td>\n",
       "      <td>38</td>\n",
       "      <td>201935</td>\n",
       "      <td>4</td>\n",
       "      <td>22015</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>0.714</td>\n",
       "      <td>21</td>\n",
       "      <td>12</td>\n",
       "      <td>0.571</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>29</td>\n",
       "      <td>34</td>\n",
       "      <td>201935</td>\n",
       "      <td>1</td>\n",
       "      <td>22015</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>0.429</td>\n",
       "      <td>31</td>\n",
       "      <td>14</td>\n",
       "      <td>0.452</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>40</td>\n",
       "      <td>201935</td>\n",
       "      <td>4</td>\n",
       "      <td>22015</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>0.833</td>\n",
       "      <td>14</td>\n",
       "      <td>10</td>\n",
       "      <td>0.714</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>-4</td>\n",
       "      <td>30</td>\n",
       "      <td>201935</td>\n",
       "      <td>2</td>\n",
       "      <td>22015</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>0.400</td>\n",
       "      <td>22</td>\n",
       "      <td>8</td>\n",
       "      <td>0.364</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>26</td>\n",
       "      <td>201935</td>\n",
       "      <td>5</td>\n",
       "      <td>22015</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>L</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   AST  BLK  DREB  FG3A  FG3M  FG3_PCT  FGA  FGM  FG_PCT  FTA ...  PF  \\\n",
       "0    4    0     2    10     5    0.500   27   13   0.481    8 ...   3   \n",
       "1    6    0     1     7     5    0.714   21   12   0.571    7 ...   0   \n",
       "2   13    0     3    14     6    0.429   31   14   0.452    8 ...   2   \n",
       "3    7    0     2     6     5    0.833   14   10   0.714    7 ...   3   \n",
       "4    4    2     5    10     4    0.400   22    8   0.364    7 ...   4   \n",
       "\n",
       "   PLUS_MINUS PTS Player_ID REB  SEASON_ID  STL  TOV  VIDEO_AVAILABLE  WL  \n",
       "0          21  38    201935   4      22015    0    1                1   W  \n",
       "1          29  34    201935   1      22015    0    3                1   W  \n",
       "2          23  40    201935   4      22015    4    4                1   W  \n",
       "3          -4  30    201935   2      22015    2    3                1   L  \n",
       "4          -1  26    201935   5      22015    1    6                1   L  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "harden_game_logs_2015.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you've found this helpful and/or have any other requests, shoot me an email [bradley@cardinaladvising.com](mailto:bradley@cardinaladvising.com) or post an issue on [github](http://github.com/bradleyfay/py-goldsberry)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
